import { Key } from '../shared/type'
import { hasChanged } from './../shared/base'
import { track, trigger } from '../shared/effect'

type Obj = { [k: Key]: any }

export const reactive = <T extends Obj>(target: T) => {
	return new Proxy(target, {
		get(t, p, r) {
			track(t, p)
			return Reflect.get(t, p, r)
		},
		set(t, p, v, r) {
			if (hasChanged(t[p], v)) {
				Reflect.set(t, p, v, r)
				trigger(t, p)
				return true
			}
			return false
		}
	})
}
